/* 
 * 
 * Copyright 2011 The University of Adelaide
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License. 
 *  
 */

package au.org.aekos.ands.bdbsa.web;

import static au.org.aekos.ands.global.Util.serialise;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import au.org.aekos.ands.bdbsa.data.Luprojectbasis;
import au.org.aekos.ands.bdbsa.data.Susurvey;
import au.org.aekos.ands.bdbsa.process.GenerateActivity;
import au.org.aekos.ands.bdbsa.process.GenerateCollection;
import au.org.aekos.ands.global.Schema;
import au.org.aekos.ands.global.Schema.EntityType;

/**
 * The controller for REST-oriented provision of activity and collection information
 * from the
 * <a href="http://www.environment.sa.gov.au/Knowledge_Bank/Information_data/Biological_databases_of_South_Australia">Biological Database of South Australia (BDBSA)</a>
 * of the
 * <a href="http://www.sa.gov.au">South Australian Government</a>
 * <a href="http://www.environment.sa.gov.au">Department of Environment and Natural Resources</a>.
 */
@RequestMapping("/")
@Controller
public class BDBSA {
	private Schema schema;
	private List<String> activities, collections;

	/**
	 * Instantiates a new controller for loading BDBSA information. Auxiliary and overlay
	 * metadata is provided by the metadata properties located on the classpath.
	 *
	 * @throws IOException Signals that an I/O exception has occurred.
	 */
	public BDBSA() throws IOException {
		schema = new Schema(getClass().getClassLoader().getResourceAsStream("metadata.properties"));

		loadActivities();
		loadCollections();
	}
	
	/**
	 * Load activities.
	 */
	private void loadActivities() {
		activities = new ArrayList<String>(schema.getExtra(EntityType.ACTIVITY));

		for (Luprojectbasis basis : Luprojectbasis.findAllLuprojectbases()) {
			if (!schema.isIgnored(EntityType.ACTIVITY, basis.getBasiscode())) {	
				activities.add(basis.getBasiscode());
			}
		}
	}
	
	/**
	 * Load collections.
	 */
	private void loadCollections() {
		collections = new ArrayList<String>(schema.getExtra(EntityType.COLLECTION));
		
		for (Susurvey survey : Susurvey.findAllSusurveys()) {
			if (!schema.isIgnored(EntityType.COLLECTION, Integer.toString(survey.getSurveynr().intValue()))) {
				collections.add(survey.getSurveynr().toString());
			}
		}
	}
	
	// Activity

    /**
	 * Returns the activity identifiers.
	 *
	 * @return the identifiers.
	 */
	@RequestMapping(value = "/activities", method = RequestMethod.GET)
    public @ResponseBody Collection<String> listActivities() {
    	return activities;
    }
    
    /**
     * Returns the specified activity.
     *
     * @param id the activity identifier.
     * @return the activity (as a RIF-CS XML string), if one corresponds to that identifier, otherwise null.
     */
    @RequestMapping(value = "/activity/{id:.*}", method = RequestMethod.GET)
    public @ResponseBody String getActivity(@PathVariable String id) {
    	if (!activities.contains(id)) { return null; }
    	
    	return serialise(new GenerateActivity(schema).generateActivity(Luprojectbasis.findLuprojectbasis(id)));
    }
    
    // Collection
    
    /**
	 * Returns the collection identifiers.
     *
     * @return the identifiers.
     */
    @RequestMapping(value = "/collections", method = RequestMethod.GET)
    public @ResponseBody Collection<String> listCollections() {
    	return collections;
    }
    
    /**
     * Returns the specified collection.
     *
     * @param id the collection identifier.
     * @return the collection (as a RIF-CS XML string), if one corresponds to that identifier, otherwise null.
     */
    @RequestMapping(value = "/collection/{id:.*}", method = RequestMethod.GET)
    public @ResponseBody String getCollection(@PathVariable String id) {
    	if (!collections.contains(id)) { return null; }

    	GenerateCollection generate = new GenerateCollection(schema);

    	if (schema.getExtra(EntityType.COLLECTION).contains(id)) {
    		return serialise(generate.generateExtraCollection(id));
    	} else {
    		return serialise(generate.generateCollection(Susurvey.findSusurvey(new BigDecimal(id))));
    	}
    }
    
    // No Party        
}
